package icu.cocoon.work.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import icu.cocoon.dao.base.BaseService;
import icu.cocoon.work.entity.WechatArticle;
import icu.cocoon.work.entity.dto.WechatArticleDTO;
import icu.cocoon.work.enums.ApproveStatus;
import icu.cocoon.work.mapper.WechatArticleMapper;
import icu.cocoon.work.pojo.param.StudyInfoParam;
import icu.cocoon.work.service.WechatArticleService;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Cocoon
 * @since 2020-07-30
 */
@Service
public class WechatArticleServiceImpl extends BaseService<WechatArticleMapper, WechatArticle> implements
    WechatArticleService {

  @Override
  public Map shareArticle(String token, Long id) {
    WechatArticle article = getById(id);
    String url = "http://api.xinshuixueyuan.com/message/article/recommendConsult/1";
    RestTemplate restTemplate = new RestTemplate();
    Map<String, Object> param = new HashMap<>(16);
    param.put("link", article.getUrl().replace("http://", "https://"));
    param.put("reason", "aaa");
    param.put("infoType", 0);
    HttpHeaders headers = new HttpHeaders();
    headers.set("token", token);
    HttpEntity<Map<String, Object>> httpEntity = new HttpEntity<>(param, headers);
    ResponseEntity<Map> entity = restTemplate.exchange(url, HttpMethod.POST, httpEntity, Map.class);
    article.setStatus(ApproveStatus.PASSED);
    update(article);
    return entity.getBody();
  }

  @Override
  public List<WechatArticleDTO> getWechatArticleDTOList(int page, String keyword, String token, String cookie)
      throws UnsupportedEncodingException {
    RestTemplate restTemplate = new RestTemplate();
    ObjectMapper mapper = new ObjectMapper();

    String url = "https://mp.weixin.qq.com/cgi-bin/operate_appmsg?sub=check_appmsg_copyright_stat";
    int size = 10;
    Map<String, String> param = new HashMap<>(32);
    param.put("token", token);
    param.put("url", URLEncoder.encode(keyword, Charset.defaultCharset().displayName()));
    param.put("begin", (page * size) + "");
    param.put("count", ((page + 1) * size) + "");
    param.put("random", "0.3506442100374547");
    param.put("allow_reprint", "0");
    param.put("f", "json");
    param.put("ajax", "1");
    param.put("lang", "zh_CN");
    StringBuilder body2 = new StringBuilder();
    param.forEach((k, v) -> body2.append("&").append(k).append("=").append(v));
    HttpHeaders headers = new HttpHeaders();
    headers.set("referer",
        "https://mp.weixin.qq.com/cgi-bin/appmsg?t=media/appmsg_edit_v2&action=edit&isNew=1&type=10&share=1&token=1352707995&lang=zh_CN");
    headers.set("Cookie", cookie);
    HttpEntity<String> httpEntity = new HttpEntity<>(body2.substring(1), headers);

    ResponseEntity<String> entity = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
    Map<String, String> map = null;
    try {
      map = mapper.readValue(entity.getBody(), Map.class);
    } catch (JsonProcessingException e) {
      e.printStackTrace();
    }
    Object listObj = map.get("list");
    List<Map<String, String>> mapList = (List<Map<String, String>>) listObj;
    List<WechatArticleDTO> result = new ArrayList<>();
    for (Map<String, String> stringMap : mapList) {
      WechatArticleDTO dto = new WechatArticleDTO();
      dto.setNickname(stringMap.get("nickname"));
      dto.setTitle(stringMap.get("title"));
      dto.setUrl(stringMap.get("url"));
      result.add(dto);
    }
    return result;
  }


  @Override
  public void save(StudyInfoParam param) {
    RestTemplate restTemplate = new RestTemplate();
    ObjectMapper mapper = new ObjectMapper();
    String url = "http://api.kuaishanggang.com/course/enterpriseCourseInfo/savePersonStudyInfo";
    HttpHeaders headers = new HttpHeaders();
    headers.set("token", param.getToken());
    Map<String, Object> body = new HashMap<>(16);
    body.put("classHour", "1");
    body.put("courseId", param.getCourseId());
    body.put("courseSectionIds", param.getCourseSectionIds());
    body.put("looktime", param.getLookTime());
    body.put("sectionTime", param.getSectionTime());
    headers.setContentType(MediaType.APPLICATION_JSON);
    HttpEntity<Map<String, Object>> httpEntity = new HttpEntity<>(body, headers);
    ResponseEntity<String> entity = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
    Map<String, String> map = null;
    try {
      map = mapper.readValue(entity.getBody(), Map.class);
    } catch (JsonProcessingException e) {
      e.printStackTrace();
    }
    System.out.println(map);
  }

}
